<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>基本资料</title>
    <link rel="stylesheet" href="/assets/lib/layui/css/layui.css">
    <link rel="stylesheet" href="/assets/css/personal/basiclnformation.css">
</head>

<body>
    <div class="layui-card">
        <div class="layui-card-header">修改用户信息</div>
        <div class="layui-card-body">
            <form class="layui-form" lay-filter="form-info">
                <input type="hidden" name="id"  class="layui-input">
                <div class="layui-form-item">
                    <label class="layui-form-label">登录名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="username" readonly="readonly" required lay-verify="required"
                            placeholder="请输入标题" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">用户昵称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="nickname" required lay-verify="required|nickname" placeholder="请输入密码"
                            autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">用户邮箱</label>
                    <div class="layui-input-inline">
                        <input type="email" name="email" required lay-verify="required|email" placeholder="请输入密码"
                            autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary" id="reset">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <script src="/assets/lib/layui/layui.all.js"></script>
    <script src="/assets/lib/jquery.js"></script>
    <script src="/assets/js/baseApi.js"></script>
    <script>
        $(function () {
            const { form, layer } = layui
            //获取用户基本信息
            getUserInfo();
            //表单验证规则
            form.verify({
                nickname(value) {
                    if (value.length > 6) return '呢称长度为1-6个字符'
                }
            })

            // 提交用户信息
            $(".layui-form").on('submit', function (e) {
                e.preventDefault();
                console.log(form.val('form-info'));
                const data = form.val('form-info')
                $.ajax({
                    type:'post',
                    url:'/my/userinfo',
                    data,
                    success(res){
                        console.log(res);
                        if(res.status === 1) return layer.msg(res.message);
                        if(res.status === 200){
                            window.parent.getUserInfo();
                            return layer.msg(res.message);
                        } 
                        
                    }
                })
            })
            //重置
            $("#reset").on('click', function (e) {
                e.preventDefault();
                getUserInfo();
            })
            //获取用户信息
            function getUserInfo() {
                $.ajax({
                    type: 'get',
                    url: '/my/userinfo',
                    success(res) {
                        console.log(res);
                        if (res.status !== 0) {
                            return layer.msg('获取信息失败!')
                        }
                        form.val('form-info', res.data)
                    }
                })
            }

        })

    </script>
</body>

</html>